import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Catygory } from './catygory.interface';
import { Model } from 'mongoose';
import { CreateCatygoryDTO, EditCatygoryDTO } from './category.dto';

@Injectable()
export class CatygoryService {
    constructor(@InjectModel('catygory') private readonly catygoryModel: Model<Catygory>)  {}

    async findAll(): Promise<Catygory[]> {
        return await this.catygoryModel.find();
    }

    async findWhere(filter?: object): Promise<Catygory[]> {
        return await this.catygoryModel.find(filter);
    }

    async find(id: string): Promise<Catygory> {
        return await this.catygoryModel.findById(id);
    }
    
    async addone(body: CreateCatygoryDTO): Promise<Catygory> {
        return await this.catygoryModel.create(body);
    }

    async editone(id: string, body: EditCatygoryDTO): Promise<Catygory> {
        return await this.catygoryModel.findByIdAndUpdate(id, body);
    }
    
    async delete(id: string): Promise<Catygory> {
        return await this.catygoryModel.findByIdAndDelete(id);
    }}
